package com.fuhao.member.mapper;

import com.fuhao.member.entity.MemberEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.repository.query.Param;


/**
* @Description:    会员DAO
* @Author:         RichZhang
*/
public interface UserMapper {

	/**
	 * 注册
	 * @param userEntity
	 * @return
	 */
	@Insert("INSERT INTO `mail_user` VALUES (null,#{mobile}, #{email}, #{password}, #{userName}, null, null, null, '1', null, null, null);")
	int register(MemberEntity userEntity);

	/**
	 * 查询用户是否存在
	 */
	@Select("SELECT * FROM mail_user WHERE MOBILE=#{mobile};")
	MemberEntity existMobile(@Param("mobile") String mobile);

	/**
	 * 登录
	 */
	@Select("SELECT USER_ID AS USERID ,MOBILE AS MOBILE,EMAIL AS EMAIL,PASSWORD AS PASSWORD, USER_NAME AS USERNAME ,SEX AS SEX ,AGE AS AGE ,CREATE_TIME AS CREATETIME,IS_AVALIBLE AS ISAVALIBLE,PIC_IMG AS PICIMG,QQ_OPENID AS QQOPENID,WX_OPENID AS WXOPENID FROM mail_user WHERE MOBILE=#{arg0} and password=#{arg1};")
	MemberEntity login(@Param("mobile") String mobile, @Param("password") String password);

	/**
	 * 根据用户id查询
	 */
	@Select("SELECT USER_ID AS USERID ,MOBILE AS MOBILE,EMAIL AS EMAIL,PASSWORD AS PASSWORD, USER_NAME AS USERNAME ,SEX AS SEX ,AGE AS AGE ,CREATE_TIME AS CREATETIME,IS_AVALIBLE AS ISAVALIBLE,PIC_IMG AS PICIMG,QQ_OPENID AS QQOPENID,WX_OPENID AS WXOPENID"
			+ " FROM mail_user WHERE user_Id=#{userId}")
	MemberEntity findByUserId(@Param("userId") Long userId);

	@Select("SELECT USER_ID AS USERID ,MOBILE AS MOBILE,EMAIL AS EMAIL,PASSWORD AS PASSWORD, USER_NAME AS USERNAME ,SEX AS SEX ,AGE AS AGE ,CREATE_TIME AS CREATETIME,IS_AVALIBLE AS ISAVALIBLE,PIC_IMG AS PICIMG,QQ_OPENID AS QQOPENID,WX_OPENID AS WXOPENID"
			+ " FROM mail_user WHERE qq_openid=#{qqOpenId}")
	MemberEntity findByOpenId(@Param("qqOpenId") String qqOpenId);

	@Update("update mail_user set QQ_OPENID =#{0} WHERE USER_ID=#{1}")
	int updateUserOpenId(@Param("qqOpenId") String qqOpenId, @Param("userId") Long userId);


}
